/*  This file is part of Basepairs, an application for calculations on
    DNA Sequence polymorphism.
    Copyright (C) 2013 Kim Lindgren

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/


package basepairs;

import java.util.ArrayList;

public class Utility {
    public static double calcHarmonic(double n){
        double sum = 1.0;
        
        for(double x = 2.0; x <= n-1; x++){
            sum = sum + 1/x;
        }
        
        return sum;
    }
    
    public static boolean arrayContains(String[] array, String item){
        for(int x = 0; x < array.length; x++){
            if(array[x].equals(item)){
                return true;
            }
        }
        
        return false;
    }
    
    private String replaceAtIndex(String original, char replacement, int index){
        StringBuilder newString = new StringBuilder(original);
        
        newString.setCharAt(index, replacement);
        
        return newString.toString();
    }
    
    private double averageLength(ArrayList<String> list){
        double avg = 0.0;
        
        for(int x = 0; x < list.size(); x++){
            avg = avg + (double)list.get(x).length();
        }
        
        avg = avg/list.size();
        
        return avg;
    }
    
    public static double calcVariance(double avg, ArrayList<Double> list){
        double var = 0.0;
        
        for(int x = 0; x < list.size(); x++){
            var = var + Math.pow(Math.abs(list.get(x)-avg), 2);
        }
        
        var = var/(double)list.size();
        
        return var;
    }
    
    public static double sumList(ArrayList<Double> list){
        double sum = 0.0;
        int length = list.size();
        
        for(int x = 0; x < length; x++){
            sum = sum + list.get(x);
        }
                
        return sum;
    }
}
